package main

import (
	"fmt"
	"net"
	"os"
	"time"
)

func main(){
	service:=":7001"
	udpAddr,err:=net.ResolveUDPAddr("udp4",service)
	checkError(err)
	conn,err:=net.ListenUDP("udp",udpAddr)
	checkError(err)
	for {
		handleClient(conn)
	}
}

func handleClient(conn *net.UDPConn){
	var buf [512]byte
	_,addr,err:=conn.ReadFromUDP(buf[0:])
	if err!=nil{
		return
	}
	daytime:=time.Now().String()
	fmt.Println("Write UDP start ...")
	conn.WriteToUDP([]byte(daytime),addr)
	fmt.Println("Write UDP  end  ...")
}

func checkError(err error){
	if err != nil {
		fmt.Fprintf(os.Stderr,"Fatail error:%s",err.Error())
		os.Exit(1)
	}
}
